
let time = new Date();
let timer;
let minute = 0;
let seconds = 0;
let uuid;
let imgBase64;
// let login=localStorage.getItem('login');
let tel=localStorage.getItem('tel');
var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;//正则表达式判断是否是有效手机号
// let urlbase = "http://localhost:9999/"
let urlbase  = "https://api.mpen.com.cn/"
// let login=true;
if (minute < 10 & minute >= 0) {
    minute = '0' + minute
}
function getTime() {
    timer = setInterval(() => {
        seconds = seconds - 1;
        minute = Number(minute);
        if (seconds < 10 & seconds >= 0) {
            seconds = '0' + seconds;
        } else if (seconds < 0) {
            seconds = 59;
            minute = minute - 1;
        }
        if (minute < 10 & minute >= 0) {
            minute = '0' + minute
        } else if (minute < 0) {
            clearInterval(timer);
            $('#login').css({ display: 'block' })
            // getORCodeUUidPic();
            // getORCodeUUid();
            return;
        }
        $('#second').text(seconds);
        $('#min').text(minute);
    }, 1000);
}
// 倒计时登录
if (tel === null|| !myreg.test(tel)) {
    $('#time').css({ display: 'none' });
    $('#second').text(seconds);
    $('#min').text(minute);
    getTime();
}
// 强制登录
if(tel===null||!myreg.test(tel)){
    $('#login').show();
    getORCodeUUid();
}

$('#refresh').click(getORCodeUUid);
$('#test').click(getORCodeUUid);//测试
// 获取uid和二维码图片
function getORCodeUUid() {
    $('.invalid').hide();
    $('#loading').show();
    $('#login_img').hide();
    return new Promise((resolve, reject) => {
        $.ajax({
            url: urlbase+'v1/business/weChat/bindWeChat/createQrCode',
            type: 'POST',
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify({ appType: 'WYS_TEACHER', width: 200, height: 200 }),
            success: (res) => {
                if (res.data) {
                    let date=60;
                    let demo=setInterval(() => {
                       date=date-1;
                       if(date==0){//二维码失效
                        clearInterval(demo);
                        getORCodeUUidPic();
                        
                       }
                    }, 1000);
                    imgBase64 = 'data:image/png;base64,' + window.btoa(String.fromCharCode(...new Uint8Array(res.data.qrCodeBytes)));
                    $('#login_img').attr({ src: imgBase64 });
                    $('#login').css({display:'block'});
                    $('#loading').css({ display: 'none' });
                    $('#login_img').css({display:'block'});
                    $('.login-img-con').css({display:'block'})
                    uuid = res.data.uid;
                    getUserCon();
                    resolve(uuid);
                    
                }
            },
            error: (err) => {
                $('.invalid').show();
                $('.qr-code-err').show();
                $('.qr-code-invalid').hide();
                $('.login-err').hide();
                $('#loading').hide();
            }
        })
    })
}
// 二维码失效
 function getORCodeUUidPic() {
    $.ajax({
        url:  urlbase+'v1/business/weChat/notifyQrCodeExpired?uid=' + uuid,
        
        type: 'GET',
        contentType: "application/json",
        success: async (res) => {
            if(res.data==true){
                $('.invalid').show();
                $('.qr-code-err').hide();
                $('.qr-code-invalid').show();
                $('.login-err').hide();
            }
            
        },
        error: (err) => {
            console.log(err);
        }
    })
}
// 获取客户端登陆信息
async function getUserCon() {
    
    $.ajax({
        url:  urlbase+'v1/business/weChat/getWebChatLoginInfo?uid=' + uuid,
        type: 'GET',
        contentType: "application/json",
        success: (res) => {
            if(res.data.state==='UNAUTHORIZED'){//用户未扫码登陆
                setTimeout(() => {
                    getUserCon(); 
                }, 5000);
            }else if(res.data.state==='INVALIDATION'){//二维码失效
                getORCodeUUidPic();
            }else if(res.data.state==='EXCEPTION'){//异常登陆情况
                $('.invalid').show();
                $('.qr-code-err').hide();
                $('.qr-code-invalid').hide();
                $('.login-err').show();
            }else if(res.data.state==='SUCCESS'){//登录成功
                localStorage.setItem('tel',res.data.data.loginId)
                $('#time').css({display:'none'});
                $('#login').css({display:'none'});
                getORCodeUUidPic();
            }
            
        },
        error: (err) => {
           
        }
    })
}

$('#close_login').click(function(){
    window.close();
})